﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;using System.Text;
using System.Data.Linq;
using System.Transactions;
using LinqToSqlExtensions;using System.ServiceModel;
using System.Collections;
using System.Collections.Generic;
using CardsServer.DAL;

namespace CardsServer.WCF
{
	public partial interface IService
{
[OperationContract]
string model_Add(model model ,int ACID);


[OperationContract]
string model_Update(model model ,int ACID);


[OperationContract]
string modelBymodelid_Delete(int modelid,int ACID);


[OperationContract]
model modelBymodelid_Get(int modelid,int ACID );


[OperationContract]
List<model> model_Get(string sortBy,int ACID);




}
public partial class Service : IService
{
#region  添加model表数据
/// <summary>
/// 添加model
/// </summary>
/// <param name="model">model实体</param>
/// <returns>空或错误</returns>
public string model_Add(model model,int ACID)
{
if(model.modelid==null )
{
return "『模块ID』不能为空！";
}
if(string.IsNullOrEmpty(model.ModelName))
{
return "『模块名称』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.model.Count(p=>p.modelid==model.modelid)>0)
{
return "主键已存在！";
}
if(ctx.model.Count(p=>p.ModelName==model.ModelName)>0)
{
return "模块名称已存在！";
}
ctx.model.InsertOnSubmit(model);
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 添加失败!错误详细信息：" + ex.Message;
}
}
}
#endregion

#region  添加model表数据事物用重构
/// <summary>
/// 添加model事物用重构
/// </summary>
/// <param name="model">model实体</param>
/// <returns>空或错误</returns>
private string model_Add(model model,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
if(model.modelid==null )
{
return "『模块ID』不能为空！";
}
if(string.IsNullOrEmpty(model.ModelName))
{
return "『模块名称』不能为空！";
}
if(ctx.model.Count(p=>p.modelid==model.modelid)>0)
{
return "主键已存在！";
}
if(ctx.model.Count(p=>p.ModelName==model.ModelName)>0)
{
return "模块名称已存在！";
}
ctx.model.InsertOnSubmit(model);
return string.Empty;
}
#endregion



#region  修改model表数据
/// <summary>
/// 修改model
/// </summary>
/// <param name="model">model实体</param>
/// <returns>空或错误</returns>
public string model_Update(model model,int ACID)
{
if(model.modelid==null )
{
return "『模块ID』不能为空！";
}
if(string.IsNullOrEmpty(model.ModelName))
{
return "『模块名称』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.model.Count(p=>p.modelid==model.modelid)!= 1)
{
return "未找到指定记录或记录不唯一！";
}
model modelmodel=ctx.model.Single(p => p.modelid==model.modelid);
if(model.modelid!=null )
{
modelmodel.modelid=model.modelid;
}
if(!string.IsNullOrEmpty(model.ModelName))
{
modelmodel.ModelName=model.ModelName;
}
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 修改失败!错误详细信息：" + ex.Message;
}
}
}
#endregion



#region  删除model表Bymodelid数据
/// <summary>
/// 删除modelBymodelid
/// </summary>
/// <param name="modelid">模块ID</param>
/// <returns>空或错误</returns>
public string modelBymodelid_Delete(int modelid,int ACID)
{
if(modelid==null )
{
return "『模块ID』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.model.Count(p=>p.modelid==modelid)!=1)
{
return null;
}
model model=ctx.model.Single(p => p.modelid== modelid);
ctx.model.DeleteOnSubmit(model);
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 删除失败!错误详细信息：" + ex.Message;
}
}
}
#endregion

#region  删除model表Bymodelid数据[事物用重构]
/// <summary>
/// 删除modelBymodelid[事物用重构]
/// </summary>
/// <param name="modelid">模块ID</param>
/// <returns>空或错误</returns>
private string modelBymodelid_Delete(int modelid,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
if(modelid==null )
{
return "『模块ID』不能为空！";
}
if(ctx.model.Count(p=>p.modelid==modelid)!=1)
{
return null;
}
model model=ctx.model.Single(p => p.modelid== modelid);
ctx.model.DeleteOnSubmit(model);
return string.Empty;
}
#endregion



#region  获取model表Bymodelid数据
/// <summary>
/// 获取modelBymodelid
/// </summary>
/// <param name="modelid">模块ID</param>
/// <returns>当前表</returns>
public model modelBymodelid_Get(int modelid,int ACID)
{
if(modelid==null )
{
return null;
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.model.Count(p=>p.modelid==modelid)!=1)
{
return null;
}
return ctx.model.SingleOrDefault(p=>p.modelid==modelid);
}
catch (Exception ex)
{
return null;
}
}
}
#endregion



#region  获取model表数据
/// <summary>
/// 获取model
/// </summary>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<model></returns>
public List<model> model_Get(Expression<Func<model, bool>> predicate , string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.model.ToList();
}
 else 
{
return ctx.model.OrderBy(sortBy).ToList();
}
}
else
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.model.Where(predicate).ToList();
}
 else 
{
return ctx.model.Where(predicate).OrderBy(sortBy).ToList();
}
}
}
catch (Exception ex)
{
return null;
}
}
}
#endregion


#region  获取model表数据
/// <summary>
/// 获取model
/// </summary>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<model></returns>
public List<model> model_Get(string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
return model_Get(null,sortBy, ACID);
}
catch (Exception)
{
return null;
}
}
}
#endregion


#region  获取model表数据计数
/// <summary>
/// 获取model表数据计数
/// </summary>
/// <returns>List<model></returns>
public int model_GetCount(Expression<Func<model, bool>> predicate,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
return ctx.model.Count();

}
 else
{
return ctx.model.Where(predicate).Count();
}
}
catch 
{
return 0;
}
}
}
#endregion




#region  获取model分页表数据
/// <summary>
/// 获取model分页表数据
/// </summary>
/// <param name="pageIndex">当前页</param>
/// <param name="pageSize">页大小</param>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<model></returns>
public List<model> model_Getpage(int pageIndex, int pageSize,Expression<Func<model, bool>> predicate ,string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.model.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
 else 
{
return ctx.model.OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
}
else
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.model.Where(predicate).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
 else 
{
return ctx.model.Where(predicate).OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
}
}
catch (Exception ex)
{
return null;
}
}
}
#endregion


#region  批量修改model表数据
/// <summary>
/// 批量修改model
/// </summary>
/// <param name="predicate">Expression<Func<model, bool>></param> 查找表达式树
/// <param name="updater">Expression<Func<model,model>> 更新表达式树</param>
/// <param name="returns">影响记录数</param>
public int model_batchUpdate(Expression<Func<model, bool>> predicate,Expression<Func<model, model>> updater,int ACID)
{
using (TransactionScope ts=new TransactionScope())
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
int ok = ctx.model.Update(predicate, updater);
ts.Complete();
 return ok;
}
catch (Exception ex)
{
return 0;
}
}
}
}
#endregion


#region  批量修改model表数据事物用重构
/// <summary>
/// 批量修改model事物用重构
/// </summary>
/// <param name="predicate">Expression<Func<model, bool>></param> 查找表达式树
/// <param name="updater">Expression<Func<model,model>> 更新表达式树</param>
/// <param name="ctx">数据上下文</param>
/// <param name="returns">影响记录数</param>
private int model_batchUpdate(Expression<Func<model, bool>> predicate,Expression<Func<model, model>> updater,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
int ok = ctx.model.Update(predicate, updater);
return ok;
}
#endregion


#region  批量删除model表数据
/// <summary>
/// 批量删除model
/// </summary>
/// <param name="predicate">Expression<Func<model, bool>></param> 查找表达式树
/// <param name="returns">影响记录数</param>
public int model_batchDel(Expression<Func<model, bool>> predicate,int ACID)
{
using (TransactionScope ts=new TransactionScope())
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
int ok = ctx.model.Delete(predicate);
ts.Complete();
 return ok;
}
catch (Exception ex)
{
return 0;
}
}
}
}
#endregion


#region  批量删除model表数据事物用重构
/// <summary>
/// 批量删除model事物用重构
/// </summary>
/// <param name="predicate">Expression<Func<model, bool>></param> 查找表达式树
/// <param name="ctx">数据上下文</param>
/// <param name="returns">影响记录数</param>
private int model_batchDel(Expression<Func<model, bool>> predicate,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
int ok = ctx.model.Delete(predicate);
return ok;
}
#endregion


}
}

